<?php 

/**
 * 登录控制器
 */
class MemberController extends Controller{
	private $model;
	
	public function __init(){
	    $this->model = K('User');
	}
    /**
     *  登录页面
     */
    public function login(){
       $this->display(); 
    }
	
	public function reg(){
		if (IS_POST){
			if (!$this->model->addData()){
				$this->error($this->model->error);
			}
			$this->success('注册成功', U('Index/Index'));
		}
		$this->display();
	}
	
	public function out(){
	    session(null);
		go(U('Index/Index'));
	}
	
	public function changeUser(){
	    session(null);
		go(U('Member/Login'));
	}
	
	public function code(){
	    $code = new Code;
		$code->show();
	}
	
	public function ajaxCheckUser(){
		$username = Q('post.username');
		$num = $this->model->where("username='". Q('post.username') ."'")->count();		
		$result = $num;
		$this->ajax($result);
	}
	
	public function ajaxLogin(){
		if (!IS_AJAX){
			go(U('Index/Index'));
		}
		
		$result = array();
		$code = Q('post.code', '', "strtoupper");
		if ($code != $_SESSION['code']){
			$result['errno'] = 1;
			$result['error'] = '验证码不正确';
		}else{
			$userInfo = $this->model->where("username='". Q('post.username') ."'")->find();
			if (!$userInfo){
				$result['errno'] = 2;
				$result['error'] = '没有该用户';
			}else{
				$password = Q('post.password','','md5');
				if ($password != $userInfo['password']){
					$result['errno'] = 3;
					$result['error'] = '用户名和密码不匹配';
				}else{
					session('uid', $userInfo['uid']);
					session('uname', $userInfo['username']);
					$result['errno'] = 0;
				}
			}
		}
		$this->ajax($result);
	}
 }
 
 
 
 
 
 
 
 
 
 
 


 ?>